<?php
/*
 * Function:File cache
 * Project :SuperEMall
 *
 * Dreamxyp(QQ:31996798) - GZ - PHP - cache.class.php
 * Coding  :xiayiping[xiayiping_xyp@163.com]
 * Created on 2007-12-20
 */
class cachearray
{
	static protected $fileDir 	 = DATACACHE_DIR;
	static protected $dir        = "array";
	static protected $time  	 = 300;
	static protected $level	 	 = 0;
	
	static protected $cache;
	
	static public function get($key)
	{
		$rs = self::$cache[$key."|".self::$dir];
		if($rs)
		{
		    return $rs;
		}else
		{
		    $filename = self::getFileName($key);
		    if(file_exists($filename) && filemtime($filename)>time()-self::$time && filesize($filename)>4)
		    {
		        @include_once $filename;
		        return $rs?$rs:false;
		    }else
		    {
		        return false;
		    }
		}
	}
	/*
	 * 功能:寫
	 */
	static public function put($key,$var,$array_key=false)
	{
		$filename = self::getFileName($key);
		if($array_key)
		{
		    $rs = self::get($key);
		    $rs[$array_key] = $var;
		    $rs = g::enarray($rs);
		}else
		{
		    $rs = is_array($var)?g::enarray($var):'".$var."';
		}
		g::write($filename,"<"."? \$rs={$rs}; ?".">");
	}
	/*
	 * 功能:刪
	 */
	static public function del($key)
	{
		$filename = self::getFileName($key);
		if(file_exists($filename))unlink($filename);
	}
	/*
	 * 功能:設定
	 */
	static public function set($config)
	{
		if(is_array($config))
		{
			foreach($config as $k => $v)
			{
				if(isset(self::$$k))self::$$k = $v;
			}	
		}
	}
	/*
	 * 功能:得到文件名
	 */
	static public function getFileName($key)
	{
		$md5 = abs(crc32($key));
		if(self::$level<=5)
		{
			$filename =  self::$fileDir.self::$dir."/";  
		}elseif(self::$level<=30)
		{
		    $VarOrd1 = $md5%30;
		    $filename =  self::$fileDir.self::$dir."/".$VarOrd1."/"; 
		}elseif(self::$level<=300)
		{
		    $VarOrd1 = substr($md5,-2,1);	
			$VarOrd2 = substr($md5,-1,1);
			$filename =  self::$fileDir.self::$dir."/".$VarOrd1."/".$VarOrd2."/"; 
		}else
		{
		    $VarOrd1 = substr($md5,-4,2);	
			$VarOrd2 = substr($md5,-2,2);
			$filename =  self::$fileDir.self::$dir."/".$VarOrd1."/".$VarOrd2."/";
		}
		g::dir($filename,true);
		$filename.= $md5;
		return $filename;
	}
}

?>